package cn.com.gas.controller;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import org.springframework.stereotype.Controller;
import io.swagger.annotations.ApiOperation;
import cn.com.common.constants.Constants;
import cn.com.common.result.DataResult;
import cn.com.gas.entity.GasStationinfoEntity;
import cn.com.gas.entity.GasStationinfoForecastEntity;
import cn.com.gas.service.GasStationinfoForecastService;
import cn.com.gas.service.GasStationinfoService;
import cn.com.system.entity.SysDept;
import cn.com.system.service.DeptService;
import cn.com.system.service.impl.HttpSessionService;
import cn.com.util.TsBqUtils;

/**
 * 销售统计
 *
 * @author shaochangyang
 * @email l2l84o7o5@live.com
 * @date 2024-04-15 20:42:03
 */
@Controller
@RequestMapping("/gas/salestatistics")
public class GasSaleStatisticsController {
	@Autowired
	private GasStationinfoForecastService gasStationinfoForecastService;
	@Autowired
	private GasStationinfoService gasStationinfoService;
	@Autowired
	private HttpSessionService httpSessionService;
	@Autowired
	private DeptService deptService;

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@ApiOperation(value = "单位加油站销售情况统计表")
	@PostMapping("/list")
	@ResponseBody
	public DataResult list(@RequestBody GasStationinfoForecastEntity gasStationinfoForecast) throws ParseException {

		String currentDeptId = httpSessionService.getCurrentDeptId();
		// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		Page page = new Page(gasStationinfoForecast.getPage(), gasStationinfoForecast.getLimit());

		String deptId = gasStationinfoForecast.getTaxorgcode();
		// 查询加油站
		LambdaQueryWrapper<SysDept> queryWrapperDept = Wrappers.lambdaQuery();

		if (TsBqUtils.isNotNull(deptId)) {
			queryWrapperDept.eq(SysDept::getId, deptId);

		} else {
			if (Constants.TAX_COUNTES.equals(TsBqUtils.getTaxOrgLevel(currentDeptId))) {
				// 县区局
				queryWrapperDept.eq(SysDept::getId, currentDeptId);
			} else if (Constants.TAX_CITIES.equals(TsBqUtils.getTaxOrgLevel(currentDeptId))) {
				// 市局 
				queryWrapperDept.like(SysDept::getId, currentDeptId.substring(0, 5));
			}else{
				// 省局
			}
			
		}
		
		IPage<SysDept> iPage = deptService.page(page, queryWrapperDept);
		
		List<SysDept> records = iPage.getRecords();
		
		for (SysDept dept : records) {
			String id = dept.getId();
			
			LambdaQueryWrapper<GasStationinfoForecastEntity> queryWrapper = Wrappers.lambdaQuery();
			
			if (Constants.TAX_COUNTES.equals(TsBqUtils.getTaxOrgLevel(id))) {
				// 县区局
				queryWrapper.eq(GasStationinfoForecastEntity::getTaxorgcode, id);
			} else if (Constants.TAX_CITIES.equals(TsBqUtils.getTaxOrgLevel(id))) {
				// 市局 
				queryWrapper.like(GasStationinfoForecastEntity::getTaxorgcode, id.substring(0, 5));
			}else{
				// 省局
				
			}
			
			if (!StringUtils.isEmpty(gasStationinfoForecast.getCreateTimeStart())) {
				queryWrapper.ge(GasStationinfoForecastEntity::getTime,gasStationinfoForecast.getCreateTimeStart().trim());

			}
			if (!StringUtils.isEmpty(gasStationinfoForecast.getCreateTimeEnd())) {
				queryWrapper.le(GasStationinfoForecastEntity::getTime,gasStationinfoForecast.getCreateTimeEnd().trim());

			}
			
			List<GasStationinfoForecastEntity> list = gasStationinfoForecastService.list(queryWrapper);
			
			BigDecimal _amountGasoline = new BigDecimal("0");
			BigDecimal _amountDiesel = new BigDecimal("0");
			BigDecimal _amountPrivate = new BigDecimal("0");
			BigDecimal _amountPublic = new BigDecimal("0");
			BigDecimal _saleGasoline = new BigDecimal("0");
			BigDecimal _saleDiesel = new BigDecimal("0");
			BigDecimal _salePrivate = new BigDecimal("0");
			BigDecimal _salePublic = new BigDecimal("0");
					
			for (GasStationinfoForecastEntity entity : list) {
				String amountGasoline = entity.getAmountGasoline();
				if(TsBqUtils.isNull(amountGasoline)){
					amountGasoline = "0";
				}
				_amountGasoline = _amountGasoline.add(new BigDecimal(amountGasoline));
				String amountDiesel = entity.getAmountDiesel();
				if(TsBqUtils.isNull(amountDiesel)){
					amountDiesel = "0";
				}
				_amountDiesel = _amountDiesel.add(new BigDecimal(amountDiesel));
				String amountPrivate = entity.getAmountPrivate();
				if(TsBqUtils.isNull(amountPrivate)){
					amountPrivate = "0";
				}
				_amountPrivate = _amountPrivate.add(new BigDecimal(amountPrivate));
				String amountPublic = entity.getAmountPublic();
				if(TsBqUtils.isNull(amountPublic)){
					amountPublic = "0";
				}
				_amountPublic = _amountPublic.add(new BigDecimal(amountPublic));
				String saleGasoline = entity.getSaleGasoline();
				if(TsBqUtils.isNull(saleGasoline)){
					saleGasoline = "0";
				}
				
				_saleGasoline = _saleGasoline.add(new BigDecimal(saleGasoline));
				String saleDiesel = entity.getSaleDiesel();
				if(TsBqUtils.isNull(saleDiesel)){
					saleDiesel = "0";
				}
				_saleDiesel = _saleDiesel.add(new BigDecimal(saleDiesel));
				String salePrivate = entity.getSalePrivate();
				if(TsBqUtils.isNull(salePrivate)){
					salePrivate = "0";
				}
				_salePrivate = _salePrivate.add(new BigDecimal(salePrivate));
				String salePublic = entity.getSalePublic();
				if(TsBqUtils.isNull(salePublic)){
					salePublic = "0";
				}
				_salePublic = _salePublic.add(new BigDecimal(salePublic));
				
			}
			
			dept.setAmountGasoline(_amountGasoline.toPlainString());
			dept.setAmountDiesel(_amountDiesel.toPlainString());
			dept.setAmountPrivate(_amountPrivate.toPlainString());
			dept.setAmountPublic(_amountPublic.toPlainString());
			dept.setSaleGasoline(_saleGasoline.toPlainString());
			dept.setSaleDiesel(_saleDiesel.toPlainString());
			dept.setSalePrivate(_salePrivate.toPlainString());
			dept.setSalePublic(_salePublic.toPlainString());
			
			
			dept.setAmount(String.valueOf(_amountPrivate.add(_amountPublic)));
			dept.setSale(String.valueOf(_salePrivate.add(_salePublic)));
			
			// 计算加油站数量
			LambdaQueryWrapper<GasStationinfoEntity> queryWrapperStationinfo = Wrappers.lambdaQuery();
			
			if (Constants.TAX_COUNTES.equals(TsBqUtils.getTaxOrgLevel(id))) {
				// 县区局
				queryWrapperStationinfo.eq(GasStationinfoEntity::getTaxorgcode, id);
				
			} else if (Constants.TAX_CITIES.equals(TsBqUtils.getTaxOrgLevel(id))) {
				// 市局 
				queryWrapperStationinfo.like(GasStationinfoEntity::getTaxorgcode, id.substring(0, 5));
				
			}else{
				// 省局
				
			}
			
			queryWrapperStationinfo.eq(GasStationinfoEntity::getType, "group");
			queryWrapperStationinfo.eq(GasStationinfoEntity::getIsAnalyse, "Y");
			
			List<GasStationinfoEntity> listStationinfo = gasStationinfoService.list(queryWrapperStationinfo);
			int number = 0;
			int numberPublic = 0;
			int numberPrivate = 0;
			
			if(TsBqUtils.isNotNull(listStationinfo)){
				for (GasStationinfoEntity entity : listStationinfo) {
					String name = entity.getName();
					boolean contains = name.contains("中国");
					boolean contains1 = name.contains("中石油");
					boolean contains2 = name.contains("中石化");
					
					if (contains || contains1 || contains2) {
						// 三桶油
						numberPublic++;

					} else {
						// 地方站
						numberPrivate++;
					}
					number ++;
				}
			}
			
			dept.setNumber(number + "");
			dept.setNumberPublic(numberPublic + "");
			dept.setNumberPrivate(numberPrivate + "");
			
		}

		return DataResult.success(iPage);
	
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@ApiOperation(value = "加油站销售统计表")
	@PostMapping("/list1")
	@ResponseBody
	public DataResult list1(@RequestBody GasStationinfoForecastEntity gasStationinfoForecast) throws ParseException {

		// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		String taxorgcode = gasStationinfoForecast.getTaxorgcode();
		if(TsBqUtils.isNull(taxorgcode)){
			return DataResult.fail("taxorgcode不能为null!");
		}
		String createTimeStart = gasStationinfoForecast.getCreateTimeStart();
		if(TsBqUtils.isNull(createTimeStart)){
			return DataResult.fail("createTimeStart不能为null!");
		}
		String createTimeEnd = gasStationinfoForecast.getCreateTimeEnd();
		if(TsBqUtils.isNull(createTimeEnd)){
			return DataResult.fail("createTimeEnd不能为null!");
		}
		
		Page page = new Page(gasStationinfoForecast.getPage(), gasStationinfoForecast.getLimit());

		LambdaQueryWrapper<GasStationinfoForecastEntity> queryWrapper = Wrappers.lambdaQuery();
		
		if (Constants.TAX_COUNTES.equals(TsBqUtils.getTaxOrgLevel(taxorgcode))) {
			// 县区局
			queryWrapper.eq(GasStationinfoForecastEntity::getTaxorgcode, taxorgcode);
			
		} else if (Constants.TAX_CITIES.equals(TsBqUtils.getTaxOrgLevel(taxorgcode))) {
			// 市局 
			queryWrapper.like(GasStationinfoForecastEntity::getTaxorgcode, taxorgcode.substring(0, 5));
		}else{
			// 省局
		}
		
		
		if (!StringUtils.isEmpty(gasStationinfoForecast.getCreateTimeStart())) {
			/*queryWrapper.ge(GasStationinfoForecastEntity::getTime,
					sdf.parse(createTimeStart + " 00:00:00"));*/
			queryWrapper.ge(GasStationinfoForecastEntity::getTime,gasStationinfoForecast.getCreateTimeStart().trim());

		}
		if (!StringUtils.isEmpty(gasStationinfoForecast.getCreateTimeEnd())) {
			/*queryWrapper.le(GasStationinfoForecastEntity::getTime,
					sdf.parse(createTimeEnd + " 23:59:59"));*/
			
			queryWrapper.le(GasStationinfoForecastEntity::getTime,gasStationinfoForecast.getCreateTimeEnd().trim());

		}
		
		IPage<GasStationinfoForecastEntity> iPage = gasStationinfoForecastService.page(page,queryWrapper);
		
		List<GasStationinfoForecastEntity> records = iPage.getRecords();
		SysDept dept = deptService.getById(taxorgcode);
		
		for (GasStationinfoForecastEntity entity : records) {
			String stationinfoId = entity.getStationinfoId();
			
			GasStationinfoEntity stationinfoEntity = gasStationinfoService.getById(stationinfoId);
		
			String name = stationinfoEntity.getName();
			entity.setStationinfoName(name);
			
			String deptName = dept.getName();
			entity.setTaxorgName(deptName);
			
			
			BigDecimal _amountGasoline = new BigDecimal("0");
			BigDecimal _amountDiesel = new BigDecimal("0");
			BigDecimal _amountPrivate = new BigDecimal("0");
			BigDecimal _amountPublic = new BigDecimal("0");
			BigDecimal _saleGasoline = new BigDecimal("0");
			BigDecimal _saleDiesel = new BigDecimal("0");
			BigDecimal _salePrivate = new BigDecimal("0");
			BigDecimal _salePublic = new BigDecimal("0");
			
			
			String amountGasoline = entity.getAmountGasoline();
			if(TsBqUtils.isNull(amountGasoline)){
				amountGasoline = "0";
			}
			_amountGasoline = _amountGasoline.add(new BigDecimal(amountGasoline));
			String amountDiesel = entity.getAmountDiesel();
			if(TsBqUtils.isNull(amountDiesel)){
				amountDiesel = "0";
			}
			_amountDiesel = _amountDiesel.add(new BigDecimal(amountDiesel));
			String amountPrivate = entity.getAmountPrivate();
			if(TsBqUtils.isNull(amountPrivate)){
				amountPrivate = "0";
			}
			_amountPrivate = _amountPrivate.add(new BigDecimal(amountPrivate));
			String amountPublic = entity.getAmountPublic();
			if(TsBqUtils.isNull(amountPublic)){
				amountPublic = "0";
			}
			_amountPublic = _amountPublic.add(new BigDecimal(amountPublic));
			String saleGasoline = entity.getSaleGasoline();
			if(TsBqUtils.isNull(saleGasoline)){
				saleGasoline = "0";
			}
			
			_saleGasoline = _saleGasoline.add(new BigDecimal(saleGasoline));
			String saleDiesel = entity.getSaleDiesel();
			if(TsBqUtils.isNull(saleDiesel)){
				saleDiesel = "0";
			}
			_saleDiesel = _saleDiesel.add(new BigDecimal(saleDiesel));
			String salePrivate = entity.getSalePrivate();
			if(TsBqUtils.isNull(salePrivate)){
				salePrivate = "0";
			}
			_salePrivate = _salePrivate.add(new BigDecimal(salePrivate));
			String salePublic = entity.getSalePublic();
			if(TsBqUtils.isNull(salePublic)){
				salePublic = "0";
			}
			_salePublic = _salePublic.add(new BigDecimal(salePublic));
			
			entity.setAmount(String.valueOf(_amountPrivate.add(_amountPublic)));
			entity.setSale(String.valueOf(_salePrivate.add(_salePublic)));
			
		}
		
		return DataResult.success(iPage);
	}

	
}
